package com.bw.a2207a.view

import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.bw.a2207a.R
import com.bw.a2207a.base.BaseActivity
import com.bw.a2207a.bean.GoodsBean
import com.bw.a2207a.databinding.ActivityActivty2Binding
import com.bw.a2207a.view.adapter.GoodsAdapter2
import com.bw.a2207a.viewmodel.HomeViewModel
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayout.OnTabSelectedListener
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener

/**
 *
 * @ClassName: MainActivity2
 * @Description: java类作用描述
 * @Author: zhangyanchao
 * @CreateDate: 2024/11/1 2:06 PM
 * @Version: 1.0
 */
@Route(path = "/main/MainActivity2")
class MainActivity2 :
    BaseActivity<ActivityActivty2Binding, HomeViewModel>(R.layout.activity_activty2) {

    var page = 1;
    val size = 10
    var id = 0

    val datas = mutableListOf<GoodsBean>()
    lateinit var adapter: GoodsAdapter2
    override fun initData() {

        viewModel.goodsLiveData.observe(this) {
            if (it.code == 200) {
                if (page == 1) {
                    datas.clear()
                }
                datas.addAll(it.data)
                adapter.notifyDataSetChanged()
                //关闭刷新，加载更多，动画效果
                binding.srl.finishRefresh()
                binding.srl.finishLoadMore()
            }
        }
    }

    override fun initView() {
        //初始化tab
        binding.tab.addTab(binding.tab.newTab().setText("默认"))
        binding.tab.addTab(binding.tab.newTab().setText("销量"))
        binding.tab.addTab(binding.tab.newTab().setText("价格"))
        binding.tab.addTab(binding.tab.newTab().setText("新品"))

        binding.tab.addOnTabSelectedListener(object : OnTabSelectedListener {
            override fun onTabSelected(tab: TabLayout.Tab) {
                //处理tab的选中事件
                if (tab.position == 0) {
                    //请求 “默认 列表”
                    id = 0
                } else if (tab.position == 1) {
                    //请求 “销量 列表”
                    id = 14
                } else if (tab.position == 2) {
                    //请求 “销量 列表”
                    id = 15
                } else if (tab.position == 3) {
                    //请求 “销量 列表”
                    id = 24
                }

                //重新发送网络请求
                page = 1
                viewModel.getGoods(id, page, size)
            }

            override fun onTabUnselected(tab: TabLayout.Tab?) {
            }

            override fun onTabReselected(tab: TabLayout.Tab?) {
            }

        })


        adapter = GoodsAdapter2(R.layout.item_goods_layout2, datas)
        binding.rv.layoutManager = LinearLayoutManager(this)
        binding.rv.adapter = adapter

        adapter.setOnItemClickListener { adapter, view, position ->
            ARouter.getInstance()
                .build("/main/GoodsDetailActivity2")
                .withInt("id", datas.get(position).id)
                .navigation()
        }
        viewModel.getGoods(0, page, size)

        //处理加载更多
        binding.srl.setOnRefreshLoadMoreListener(object : OnRefreshLoadMoreListener {
            override fun onRefresh(refreshLayout: RefreshLayout) {
                //刷新，请求第一页数据
                page = 1
                viewModel.getGoods(id, page, size)
            }

            override fun onLoadMore(refreshLayout: RefreshLayout) {
                //加载更多，请求下一页
                page++
                viewModel.getGoods(id, page, size)
            }
        })


        binding.tvSearch.setOnClickListener {
            //获取，输入框内的关键字
            val et = binding.etSearch.text.toString()


            val newsList = mutableListOf<GoodsBean>()

            datas.forEach {
                if (it.goods_desc.contains(et)) {
                    newsList.add(it)
                }
            }

//            val list = datas.filter {
//                it.goods_desc.contains(et)
//            }

            adapter = GoodsAdapter2(R.layout.item_goods_layout2, newsList)
            binding.rv.layoutManager = LinearLayoutManager(this)
            binding.rv.adapter = adapter

        }
    }
}